import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:inmotion_pro/config/theme.dart';
import 'package:inmotion_pro/l10n/local.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

import 'controller.dart';

class AreaCodeView extends BaseGetView<AreaCodeController> {
  AreaCodeView({super.key});

  @override
  Widget? get backView => SizedBox(
        child: IconButton(
          icon: const Icon(Icons.close),
          onPressed: () => Get.back(),
        ),
      );

  @override
  List<Widget>? get rightViews => [
        TextButton(
          onPressed: () {
            Get.back(result: controller.state.selectedCountry.value);
          },
          child: LxText(
            local.confirm,
            color: Colors.white,
            fontWeightEnum: LxFontWeightEnum.titleMedium,
          ),
        ),
      ];

  @override
  Color? get barBackgroundColor => lxColorScheme.blockColor;

  @override
  Widget buildBody(BuildContext context) {
    return DismissKeyboardScaffold(
      resizeToAvoidBottomInset: false, // 防止键盘弹出时页面内容被顶起
      child: Scaffold(
        backgroundColor: barBackgroundColor,
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            children: [
              // 显示选中的国家
              Obx(() => Text(
                    controller.selectedCountryInfo,
                    style: const TextStyle(
                        fontSize: 18, fontWeight: FontWeight.bold),
                  )),

              const SizedBox(height: 20),

              // 搜索框
              TextField(
                decoration: InputDecoration(
                  labelText: local.search_country_region,
                  prefixIcon: const Icon(Icons.search),
                  border: const OutlineInputBorder(),
                  enabledBorder: OutlineInputBorder(
                    borderSide:
                        BorderSide(color: Colors.white.withOpacity(0.3)),
                  ),
                  focusedBorder: OutlineInputBorder(
                    borderSide: BorderSide(
                        color: Colors.white.withOpacity(0.8), width: 1.0),
                  ),
                ),
                onChanged: (value) {
                  controller.applyFilters(controller.state.filterOptions.value
                      .copyWith(keyword: value));
                },
              ),

              const SizedBox(height: 20),

              // 加载状态和错误信息
              Obx(() => controller.state.isLoading.value
                  ? const CircularProgressIndicator()
                  : Container()),

              Obx(() => controller.state.errorMessage.value.isNotEmpty
                  ? Text(
                      controller.state.errorMessage.value,
                      style: const TextStyle(color: Colors.red),
                    )
                  : Container()),

              // 国家列表
              Expanded(
                child: Obx(() => ListView.separated(
                      separatorBuilder: (BuildContext context, int index) =>
                          const Divider(),
                      itemCount: controller.state.filteredCountries.length,
                      itemBuilder: (context, index) {
                        final country =
                            controller.state.filteredCountries[index];
                        return ListTile(
                          leading: Text(country.flag ?? "",
                              style: const TextStyle(fontSize: 24)),
                          title: LxText(
                            country.name,
                            fontWeightEnum: LxFontWeightEnum.titleMedium,
                          ),
                          subtitle: LxText(
                            '${local.country_code}: ${country.countryCode}, ${local.phone_code}: ${country.phoneCode}',
                            fontWeightEnum: LxFontWeightEnum.bodyLarge,
                            color: WhiteColor.white2,
                          ),
                          onTap: () {
                            controller.selectCountry(country);
                            // 收起键盘
                            FocusScope.of(context).unfocus();
                          },
                        );
                      },
                    )),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
